package com.itheima.tlias84.controller;

import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEMPDetailDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.po.InsterEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/emps")
public class EmpController {

    @Resource
    EmpService empService;

    @GetMapping
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
        if (Objects.isNull(param.getPage())|| Objects.isNull(param.getPageSize())){
            return ResultUtil.fail("分页参数不能为空");
        }
        return empService.selectEmpByPage(param);
    }
    @DeleteMapping
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids) {
        if (ids.isEmpty()){
            return ResultUtil.fail("请先选择要删除的id");
        }
        int flag = empService.batchDelete(ids);
        return ResultUtil.success(flag);
    }
    @PostMapping
    public ResultUtil insertEmp(@Validated @RequestBody InsterEmpParam param){
        int i = empService.insertEmp(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }
    @GetMapping("/{id}")
    public ResultUtil selectEmpById(@PathVariable("id") Integer id){
        SelectEMPDetailDTO dto = empService.selectEmpById(id);
        return ResultUtil.success(dto);
    }
    @PutMapping
    public ResultUtil updateEmp(@RequestBody UpdateEmpParam param){
        int i = empService.updateEmp(param);
        return i == 0 ? ResultUtil.fail("失败") : ResultUtil.success("成功");
    }
    @GetMapping("/list")
    public ResultUtil selectAll(){
        List<SelectAllEmpDTO> list = empService.selectAll();
        return ResultUtil.success(list);
    }
}
